package com.mechat.ui.chat.profile

import android.graphics.Color
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.util.Log
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.custom.base.mvp.BasePresenterImpl
import com.mechat.Constant
import com.mechat.R
import com.mechat.adapter.PageLoaderStringAdapter
import com.mechat.adapter.UserProfileTurnOnsAdapter
import com.mechat.bean.UserProfileInfoItemBean
import com.mechat.entity.UserProfileInfoEntity
import com.mechat.util.CollectionUtils
import com.mechat.util.DateUtils
import com.mechat.util.GlideUtil
import com.mechat.util.HeightUtils
import com.mechat.util.SystemUtils
import com.mechat.view.LinearLineIndicator
import com.youth.banner.config.BannerConfig
import com.youth.banner.config.IndicatorConfig
import com.youth.banner.util.BannerUtils


/**
 * 作者：
 * 时间：
 * 描述：
 */
class UserProfileInfoPresenter : BasePresenterImpl<UserProfileInfoContract.View>(),
    UserProfileInfoContract.Presenter {

    override fun initBundle(bundle: Bundle) {
        mView?.apply {
            OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
                override fun addBody(requestBody: OkHttpBodyEntity) {
                    requestBody.setPost(Constant.chatter_user_profile_url)
                    requestBody.add("userCode",bundle.getString("userCodeFriend",""))
                }
            }, object : SDOkHttpResoutCallBack<UserProfileInfoEntity>() {
                override fun onSuccess(entity: UserProfileInfoEntity) {

                    banner.indicator = LinearLineIndicator(mActivity)
                    banner.setIndicatorMargins(
                        IndicatorConfig.Margins(
                            0, 0,
                            BannerConfig.INDICATOR_MARGIN, BannerUtils.dp2px(25f).toInt()
                        )
                    )
                    if (CollectionUtils.isNotEmpty(entity.data.images)) {
                        banner.adapter = PageLoaderStringAdapter(mActivity, entity.data.images)
                    }


                    if (CollectionUtils.isNotEmpty(entity.data.turnOnsList)){
                        val onsList = entity.data.turnOnsList
                        onsList.removeIf{
                            it.selected==0
                        }
                        turnOnsContainer.visibility=if(CollectionUtils.isNotEmpty(onsList))View.VISIBLE else View.GONE
                        turnOnsList.layoutManager = GridLayoutManager(mActivity, 2)
                        (turnOnsList.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
                        val turnOnsAdapter = UserProfileTurnOnsAdapter(onsList, mActivity,
                            object : UserProfileTurnOnsAdapter.OnListener {
                                override fun onListener(position: Int) {

                                }

                            })
                        turnOnsList.adapter = turnOnsAdapter
                    }else{
                        turnOnsContainer.visibility=View.GONE
                    }
                    userProfileOnline.visibility=if (entity.data.online==1)View.VISIBLE else View.GONE

                    if (entity.data.lat!= null && entity.data.lng!= null){
                        if(entity.data.lat!=0.0 && entity.data.lng!=0.0) {
                            userProfileAddress.visibility= View.VISIBLE
                            userProfileAddress.text = SystemUtils.getCity(mActivity, entity.data.lat, entity.data.lng)
                        }else{
                            userProfileAddress.visibility= View.GONE
                        }
                    }else{
                        userProfileAddress.visibility= View.GONE
                    }
                    if(entity.data.member) {
                        val style = SpannableStringBuilder()
                        style.append("${entity.data.nickName} , ${entity.data.age}")
                        style.setSpan(object : ClickableSpan() {
                            override fun onClick(p0: View) {
                            }
                            override fun updateDrawState(ds: TextPaint) {
                                ds.isUnderlineText = false
                                ds.color=Color.parseColor("#FAC44E")
                                ds.clearShadowLayer()
                            }
                        }, 0, entity.data.nickName.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                        userProfileName.text = style
                        userProfileName.movementMethod = LinkMovementMethod.getInstance()

                        val drawable =
                            ContextCompat.getDrawable(mActivity, R.drawable.icon_profile_crown)
                        val drawableWidth = 19
                        val drawableHeight = 14
                        drawable!!.setBounds(0, 0, drawableWidth, drawableHeight)
                        userProfileName.setCompoundDrawablesWithIntrinsicBounds(
                            drawable,
                            null,
                            null,
                            null
                        )
                    }else {
                        userProfileName.text="${entity.data.nickName} , ${entity.data.age}"
                    }

                    if(entity.data.birthday!=null && entity.data.birthday!=""){
                        val month = Integer.parseInt(DateUtils.getDateString(entity.data.birthday, "MM"))
                        val day = Integer.parseInt(DateUtils.getDateString(entity.data.birthday, "dd"))
                        if (month == 1 && day >= 20 || month == 2 && day <= 18) {
                            userStarSign.text=mActivity.getString(R.string.star_sign_1)
                            userProfileStarSignRightImg.setImageResource(R.mipmap.icon_star_sign_1)
                        } else if (month == 2 && day >= 19 || month == 3 && day <= 20) {
                            userStarSign.text=mActivity.getString(R.string.star_sign_2)

                            userProfileStarSignRightImg.setImageResource(R.mipmap.icon_star_sign_2)
                        } else if (month == 3 && day >= 21 || month == 4 && day <= 19) {
                            userStarSign.text=mActivity.getString(R.string.star_sign_3)

                            userProfileStarSignRightImg.setImageResource(R.mipmap.icon_star_sign_3)
                        } else if (month == 4 && day >= 20 || month == 5 && day <= 20) {
                            userStarSign.text=mActivity.getString(R.string.star_sign_4)

                            userProfileStarSignRightImg.setImageResource(R.mipmap.icon_star_sign_4)
                        } else if (month == 5 && day >= 21 || month == 6 && day <= 21) {
                            userStarSign.text=mActivity.getString(R.string.star_sign_5)

                            userProfileStarSignRightImg.setImageResource(R.mipmap.icon_star_sign_5)
                        } else if (month == 6 && day >= 22 || month == 7 && day <= 22) {
                            userStarSign.text=mActivity.getString(R.string.star_sign_6)

                            userProfileStarSignRightImg.setImageResource(R.mipmap.icon_star_sign_6)
                        } else if (month == 7 && day >= 23 || month == 8 && day <= 22) {
                            userStarSign.text=mActivity.getString(R.string.star_sign_7)

                            userProfileStarSignRightImg.setImageResource(R.mipmap.icon_star_sign_7)
                        } else if (month == 8 && day >= 23 || month == 9 && day <= 22) {
                            userStarSign.text=mActivity.getString(R.string.star_sign_8)

                            userProfileStarSignRightImg.setImageResource(R.mipmap.icon_star_sign_8)
                        } else if (month == 9 && day >= 23 || month == 10 && day <= 22) {
                            userStarSign.text=mActivity.getString(R.string.star_sign_9)

                            userProfileStarSignRightImg.setImageResource(R.mipmap.icon_star_sign_9)
                        } else if (month == 10 && day >= 23 || month == 11 && day <= 21) {
                            userStarSign.text=mActivity.getString(R.string.star_sign_10)

                            userProfileStarSignRightImg.setImageResource(R.mipmap.icon_star_sign_10)
                        } else if (month == 11 && day >= 22 || month == 12 && day <= 21) {
                            userStarSign.text=mActivity.getString(R.string.star_sign_11)

                            userProfileStarSignRightImg.setImageResource(R.mipmap.icon_star_sign_11)
                        } else if (month == 12 && day >= 22 || month == 1 && day <= 19) {
                            userStarSign.text=mActivity.getString(R.string.star_sign_12)
                            userProfileStarSignRightImg.setImageResource(R.mipmap.icon_star_sign_12)
                        }
                    }

                    tagContainer.visibility=if (entity.data.userType!=4)View.GONE else View.VISIBLE

                    userProfileGenderRightImg.setImageResource(if (entity.data.gender==1) R.mipmap.icon_gender_male_small else if (entity.data.gender==2) R.mipmap.icon_gender_female_small else R.mipmap.icon_gender_unknown_small)
                    userProfileGender.text=if (entity.data.gender==1) "I'm Male" else if (entity.data.gender==2) "I'm Female" else "I'm Queer"
                    userProfileLooking.text=if (entity.data.lookingFor==1) "I'm Seeking For Man" else if (entity.data.lookingFor==2) "I'm Seeking For Woman" else "I'm Seeking For Queer"
                    userProfileLookingRightImg.setImageResource(if (entity.data.lookingFor==1) R.mipmap.icon_gender_male_small else if (entity.data.lookingFor==2) R.mipmap.icon_gender_female_small else R.mipmap.icon_gender_unknown_small)


                    dividingLineOne.visibility=if (entity.data.aboutMe == null)View.GONE else View.VISIBLE
                    userProfileAboutMeTitle.visibility=if (entity.data.aboutMe == null)View.GONE else View.VISIBLE
                    userProfileIntroduction.text=entity.data.aboutMe

                    dividingLineTwo.visibility=if (CollectionUtils.isEmpty(entity.data.interests))View.GONE else View.VISIBLE
                    userProfileMyInterestsTitle.visibility=if (CollectionUtils.isEmpty(entity.data.interests))View.GONE else View.VISIBLE
                    userProfileTagCloud.visibility=if (CollectionUtils.isEmpty(entity.data.interests))View.GONE else View.VISIBLE



                    if (entity.data.height !=null){
                        heightContainer.visibility=View.VISIBLE
                        heightInches.text="${entity.data.inchHeight}"
                        heightCentimetre.text="(${entity.data.height}cm)"
                    }


                    if (entity.data.socialConnections!= null){
                        purposeContainer.visibility=View.VISIBLE
                        txtPurpose.text=entity.data.socialConnections
                        GlideUtil.setImageView(entity.data.socialConnectionsUrl,imgPurpose)
                    }else{
                        purposeContainer.visibility=View.GONE
                    }
                    if (CollectionUtils.isNotEmpty(entity.data.interests)) {
                        userProfileTagCloud.setTags(entity.data.interests)
                    }

                }


                override fun onFailure(code: Int, msg: String) {
                }
            })

        }
    }

}